package com.winning.sx.microframework.dao.sysmanage;

import com.winning.dapi.data.api.IDataAccessService;
import com.winning.dapi.data.entity.DataTable;
//import com.winning.sx.microframework.model.exception.CustomException;
import com.winning.sx.microframework.model.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by MJ on 2016-10-12.
 */
@Repository
public class RoleManageDao {

    @Autowired
    @Qualifier("dataSource")
    private IDataAccessService dataAccessService;

    public DataTable queryRoles() throws CustomException {
        String sql = "select cRoleID,iOrder,cRoleName,bState,cDescribe,cInputTime from pm_roles ORDER BY iOrder";
        try {
            return this.dataAccessService.querySql(sql);
        } catch (Exception e) {
            throw new CustomException(-2001, "角色获取失败!");
        }
    }

    public int insertRole(Map<String, Object> map) throws CustomException {
        int result = 0;
        //String cRoleID = UUID.randomUUID().toString();
        //map.put("cRoleID", cRoleID);
        map.put("validFlag","100001");
        String sql = "insert into pm_roles(cRoleID,iOrder,cRoleName,bState,cDescribe,cInputTime,validFlag) values(:cRoleID,:iOrder,:cRoleName,:bState,:cDescribe,:cInputTime,:validFlag)";
        try {
            result = this.dataAccessService.executeSqlUsingParam(sql, map);
        } catch (Exception e) {
            throw new CustomException(-2002, "角色新增失败!");
        }
        return result;
    }

    public int updateRole(Map<String, Object> map) throws CustomException {
        int result = 0;
        String sql = "update pm_roles set iOrder=:iOrder,cRoleName=:cRoleName,bState=:bState,cDescribe=:cDescribe,cInputTime=:cInputTime where cRoleID=:cRoleID";
        try {
            result = this.dataAccessService.executeSqlUsingParam(sql, map);

        } catch (Exception e) {
            throw new CustomException(-2003, "角色修改失败!");
        }
        return result;
    }

    public int deleteRole(Map<String, Object> map) throws CustomException {
        int result = 0;
        try {
            String sql = "delete from pm_userasrole where cRoleID=:cRoleID";
            result = this.dataAccessService.executeSqlUsingParam(sql, map);
            sql = "delete from pm_roleassystem where cRoleID=:cRoleID";
            result += this.dataAccessService.executeSqlUsingParam(sql, map);
            sql = "delete from pm_roles where cRoleID=:cRoleID ";
            result += this.dataAccessService.executeSqlUsingParam(sql, map);
        } catch (Exception e) {
            throw new CustomException(-2004, "角色删除失败!");
        }
        return result;
    }

    /**
     * 获取角色系统关联信息
     *
     * @param map
     * @return
     * @throws CustomException
     */
    public DataTable queryRSasso(Map<String, Object> map) throws CustomException {
        DataTable table = null;
        try {
            String sql = "SELECT cSystemID, cSystemName, CASE WHEN cSystemID IN (SELECT cSystemID" +
                    "                                                        FROM PM_RoleAsSystem" +
                    "                                                        WHERE cRoleID=:cRoleID) THEN 1" +
                    "                                    ELSE 0" +
                    "                               END AS active" +
                    "  FROM PM_SYSTEMS" +
                    "  ORDER BY iOrder";
            table = dataAccessService.querySqlUsingParam(sql, map);
        } catch (Exception e) {
            throw new CustomException(-2001, "获取数据失败");
        }
        return table;
    }

    /**
     * 保存关联信息
     *
     * @param map
     * @return
     * @throws CustomException
     */
    public int saveAsso(Map<String, Object> map) throws CustomException {
        int result = -1;
        try {
            String sql = "DELETE FROM pm_roleassystem WHERE cRoleID=:cRoleID";
            int row = -1;
            if (map.size() > 0) {
                row = dataAccessService.executeSqlUsingParam(sql, map);
            }
            if (row > -1) {
                result = 0;
                for (Map<String, Object> evnMap : (List<Map<String, Object>>) map.get("selected")) {
                    Map<String, Object> seMap = new HashMap<String, Object>();
                    seMap.put("cRoleID", map.get("cRoleID"));
                    seMap.put("cSystemID", evnMap.get("cSystemID"));
                    String sql1 = "insert into pm_roleassystem(cRoleID, cSystemID) values(:cRoleID, :cSystemID)";
                    row = dataAccessService.executeSqlUsingParam(sql1, seMap);
                    if (row > 0)
                        result++;
                }
            }
        } catch (Exception e) {
            throw new CustomException(-2003, "操作失败");
        }
        return result;
    }
}
